﻿package src{
	import src.game.Game;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	public class Main extends MovieClip{
		private var menu:MainMenu;
		private var game:Game;
		//------------------------------------------------------------------------------------------------------------
		public function Main() {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		//------------------------------------------------------------------------------------------------------------
		private function init(event:Event = null):void {
			preloader.setLoaderInfo(loaderInfo);
			preloader.addEventListener(Event.COMPLETE, onPreloaderComplete);
			preloader.addEventListener(IOErrorEvent.IO_ERROR, onPreloaderError);
		}
		//------------------------------------------------------------------------------------------------------------
		private function onPreloaderComplete(event:Event):void {  
             preloader.removeEventListener(Event.COMPLETE, onPreloaderComplete);
			 play();
       	}
		//------------------------------------------------------------------------------------------------------------
		private function onPreloaderError(event:IOErrorEvent):void {
			//@TODO - napisać funkcję obsługującą błąd przy pobieraniu
		}
		//------------------------------------------------------------------------------------------------------------
		private function createMainMenu():void {
			menu = new MainMenu();
			menu.addEventListener(MainMenu.NEW_GAME, newGame);
			menu.addEventListener(MainMenu.ABOUT, about);
			this.stage.addChild(menu);
			menu.x = menu.parent.width/2 - menu.width/2;
			menu.y = menu.parent.height - 2*menu.height;
			newGame(null);
		}
		//------------------------------------------------------------------------------------------------------------
		private function newGame(event:Event):void {
			trace("nowa gra");
			game = new Game(this.stage.stageWidth, this.stage.stageHeight);
			this.stage.addChild(game);
		}
		
		private function about(event:Event):void {
			trace("about");
		}
	}
}